/*
  createRouter 创建路由实例
  createWebHashHistory 创建 hash 路由模式
*/
import { createRouter, createWebHashHistory } from 'vue-router'

// import homeRoute from './modules/home.js'
import loginRoute from './login'

const importFn = require.context('./modules', false, /\.js$/)

const children = []
importFn.keys().map(key => children.push(...importFn(key).default))

// 映射路径和组件
const routes = [
  {
    path: '/',
    name: 'Layout',
    component: () => import('@/views/Layout.vue'),
    children
  },
  ...loginRoute
]

// 路由实例
const router = createRouter({
  history: createWebHashHistory(),
  routes,
  scrollBehavior (to, from, savedPosition) {
    const isCate = to.path.includes('category')
    return isCate
      ? new Promise((resolve, reject) =>
        setTimeout(
          () => resolve({ left: 0, top: 0, behavior: 'smooth' }),
          500
        )
      )
      : savedPosition
  }
})

// 导出实例
export default router
